package com.fingard.dsp.bank.directbank.ybt12;

import com.fingard.FGBiz;
import com.fingard.db.JDBCHelper;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.directbank.DirectBase;

import java.sql.Connection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class YBT12Base extends DirectBase {
	/**
	 * 数据批量插入中间表
	 * 
	 * @param params
	 *            请求数据明细
	 * @param p_sb
	 *            日志信息
	 * @return
	 */
	public boolean saveEachRecords(LinkedList<Object> params, StringBuilder p_sb) {
		if (FGBiz.dspCenter.dspSet.dbConnectionSet != null) {
			try {
				Connection dbConn = null;

				try {
					dbConn = FGBiz.dspCenter.dspSet.dbConnectionSet.getConnection();
					JDBCHelper.update(dbConn, "insert into BATCHPAY(Id,BkCustName,BkAcctNo,BkName,LiOperType,PbInsuSlipNo,BkAmt1,BranchCode,InsertTime,BkRetCode,BkRetMsg,Channel,PayOrCharge,TimeStamp,PbRemark1,SendDataTime,GetDataTime,BUSNBR,SQRNBR,OPTSTU,BatchNo,PROVINCE,CITY,IDTYPE,IDNUMBER,PHONE,PAYMENTCODE,AGENCYACCOUNT,TERMID) " + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", params.toArray());
					return true;
				} catch (Exception ex) {
					p_sb.append(LogHelper.getStackTrace(ex));
					ex.printStackTrace();
				} finally {
					if (dbConn != null) {
						dbConn.close();
					}
				}
			} catch (Exception ex) {
				p_sb.append(LogHelper.getStackTrace(ex));
				ex.printStackTrace();
			}
		} else {
			p_sb.append("数据库连接未配置或有误");
		}
		return false;
	}

    /**
     * 从中间表查询批量结果明细数据
     *
     * @param reqSeqID
     *            批次号
     * @return 批次结果明细数据
     * @throws Exception
     */
    public int getResultRecordCounts(String reqSeqID) throws Exception {
        if (FGBiz.dspCenter.dspSet.dbConnectionSet != null) {
            Connection dbConn = null;
            try {
                dbConn = FGBiz.dspCenter.dspSet.dbConnectionSet.getConnection();

                String sqlSelect = "select count(1) len from BATCHPAY where BUSNBR = ?";
                List<Map<String, Object>> seqList = JDBCHelper.execQuery(dbConn, sqlSelect, new Object[] { reqSeqID });

                Map<String,Object> map = seqList.get(0);
				return Integer.parseInt(map.get("LEN").toString());
            } catch (Exception ex) {
                throw (ex);
            } finally {
                if (dbConn != null) {
                    dbConn.close();
                }
            }
        } else {
            throw new Exception("数据库连接未配置或有误");
        }
    }

	/**
	 * 从中间表查询批量结果明细数据
	 * 
	 * @param reqSeqID
	 *            批次号
	 * @return 批次结果明细数据
	 * @throws Exception
	 */
	public List<Map<String, Object>> getResultRecords(String reqSeqID) throws Exception {
		if (FGBiz.dspCenter.dspSet.dbConnectionSet != null) {
			Connection dbConn = null;
			try {
				dbConn = FGBiz.dspCenter.dspSet.dbConnectionSet.getConnection();

				String sqlSelect = "select * from BATCHPAY where BUSNBR = ? and OPTSTU = 2";
				List<Map<String, Object>> seqList = JDBCHelper.execQuery(dbConn, sqlSelect, new Object[] { reqSeqID });
				return seqList;
			} catch (Exception ex) {
				throw (ex);
			} finally {
				if (dbConn != null) {
					dbConn.close();
				}
			}
		} else {
			throw new Exception("数据库连接未配置或有误");
		}
	}
	
	/**
	 * 从中间表更新批量结果明细数据中提交时间在7天以上的
	 * 
	 * @param p_sb
	 *            日志信息
	 * @return
	 */
	public boolean updateReturnStatus(StringBuilder p_sb) {
		if (FGBiz.dspCenter.dspSet.dbConnectionSet != null) {
			try {
				Connection dbConn = null;

				try {
					dbConn = FGBiz.dspCenter.dspSet.dbConnectionSet.getConnection();
					String sqlUpdate = "update BATCHPAY set OPTSTU = 3,TIMESTAMP=sysdate where SENDDATATIME < trunc(sysdate-7) and OPTSTU = 2";
					JDBCHelper.update(dbConn, sqlUpdate, new Object[] {});
					return true;
				} catch (Exception ex) {
					p_sb.append(LogHelper.getStackTrace(ex));
					ex.printStackTrace();
				} finally {
					if (dbConn != null) {
						dbConn.close();
					}
				}
			} catch (Exception ex) {
				p_sb.append(LogHelper.getStackTrace(ex));
				ex.printStackTrace();
			}
		} else {
			p_sb.append("数据库连接未配置或有误");
		}
		return false;
	}
}
